package com.zjzywl.iotSendthings.util;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("restriction")
public class ImageTransferUtil {

    private final static Logger log = LoggerFactory.getLogger(ImageTransferUtil.class);

    private static final int mosaicSize = 15;


    // 将图片文件转化为字节数组字符串，并对其转化为--->Base64编码
    public static String GetImageStr(String imgFilePath) {
        byte[] data = null;
        try {
            // 读取图片字节数组
            FileInputStream fis = new FileInputStream(imgFilePath);
            data = new byte[fis.available()];
            fis.read(data);
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // 对字节数组Base64编码
        BASE64Encoder encoder = new BASE64Encoder();
        // 返回Base64编码过的字节数组字符串
        String encode = encoder.encode(data);
        Pattern p = Pattern.compile("\\s*|\t|\r|\n");
        Matcher matcher = p.matcher(encode);
        return matcher.replaceAll("");
    }

    /**
     * 保存文件，直接以multipartFile形式
     *
     * @param multipartFile
     * @param path          文件保存绝对路径
     * @return 返回文件名
     * @throws IOException
     */
    public static String saveImg(MultipartFile multipartFile, String path, String uuid) throws IOException {
        //创建文件名
        String fileName =uuid+"."+multipartFile.getOriginalFilename().split("\\.",-1)[1];
        File file = new File(path);
        if (!file.exists()) {
            file.mkdirs();
        }
        FileInputStream fileInputStream = (FileInputStream) multipartFile.getInputStream();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(path + File.separator + fileName));
        byte[] bs = new byte[1024];
        int len;
        while ((len = fileInputStream.read(bs)) != -1) {
            bos.write(bs, 0, len);
        }
        bos.flush();
        bos.close();
        return fileName;
    }

    /**
     * @return void
     * @description 根据路径获取图片io
     * @params [response, pictureFile]
     * @author wangjie
     * @date 2019/11/12 14:16
     */
    public static void getPictureByUrl(HttpServletResponse response, String pictureFile) throws IOException {
        response.setContentType("image/jpeg ");
        FileInputStream fis = new FileInputStream(pictureFile);
        OutputStream os = response.getOutputStream();
        try {
            int count = 0;
            byte[] buffer = new byte[1024 * 1024];
            while ((count = fis.read(buffer)) != -1) {
                os.write(buffer, 0, count);
            }
            os.flush();
        } catch (Exception e) {
            log.error("获取图片出错", e);
        } finally {
            if (os != null) {
                os.close();
            }
            if (fis != null) {
                fis.close();
            }
        }
    }

}
